package com.xuan.mail.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuan.mail.domin.Email;
import com.xuan.mail.domin.Folderm;
import com.xuan.mail.domin.User;
import com.xuan.mail.service.EmailService;
import com.xuan.mail.service.UserService;
import com.xuan.mail.service.impl.EmailProducer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("email")
public class EmailController {
    @Resource
    private EmailService emailService;
    @Resource
    private EmailProducer emailProducer;
    @Resource
    private UserService userservice;

    //通过文件id获取邮件列表
    @GetMapping("/{folderId}")
    public List<Email> getEmailListByFolderId(@PathVariable("folderId") Integer folderId){

        return emailService.getEmailListByFolderId(folderId);
    }


    //通过邮件id获取邮件内容
    @GetMapping("/content/{emailId}")
    public Email getEmailContent(@PathVariable("emailId") Integer emailId){
        return emailService.getEmailContent(emailId);
    }
    //获取文件夹列表
    @GetMapping("/folder")
    public List<Folderm> getFolder(){
        return emailService.getFolder();
    }



    //分页查询列表
    @GetMapping("/list")
    public Page<Email> getEmailListFolder(@RequestParam Integer folderId,@RequestParam Integer userId ,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10")Integer pageSize){
      Page<Email> emailPage = emailService.getEmailListFolder(folderId,userId,pageNum,pageSize);
      System.out.println("page记录"+emailPage.getRecords());
        System.out.println("当前页码：" + emailPage.getCurrent());
        System.out.println("每页记录数：" + emailPage.getSize());
        System.out.println("总记录数：" + emailPage.getTotal());
        System.out.println("总页数：" + emailPage.getPages());
        System.out.println("是否有上一页：" + emailPage.hasPrevious());
        System.out.println("是否有下一页：" + emailPage.hasNext());
      return emailPage;

    }
    //分页显示所有星标邮件
    @GetMapping("/star_list")
    public Page<Email> getStarEmailList(@RequestParam Integer userId,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10")Integer pageSize){
        Page<Email> emailPage = emailService.getStarEmailList(userId,pageNum,pageSize);
        System.out.println("page记录"+emailPage.getRecords());
        System.out.println("当前页码：" + emailPage.getCurrent());
        System.out.println("每页记录数：" + emailPage.getSize());
        System.out.println("总记录数：" + emailPage.getTotal());
        System.out.println("总页数：" + emailPage.getPages());
        System.out.println("是否有上一页：" + emailPage.hasPrevious());
        System.out.println("是否有下一页：" + emailPage.hasNext());
        return emailPage;

    }
    @GetMapping("/unreadCount")
    public long getUnreadCount(@RequestParam Integer folderId,@RequestParam Integer userId){
        return emailService.countUnreadEmails(folderId,userId);
    }
    //统计星标未读
    @GetMapping("/starUnReadCount")
    public long getStarUnReadCount(@RequestParam Integer userId){
        return emailService.countStarUnreadEmails(userId);
    }
    //全部已读
    @PostMapping("/allRead")
    public String allRead(@RequestParam Integer folderId,@RequestParam Integer userId){
        emailService.allRead(folderId,userId);
        return "全部已读";
    }
    //全部已读
    @PostMapping("/allStarRead")
    public String allStarRead(@RequestParam Integer userId){
        emailService.allStarRead(userId);
        return "全部星标邮件已读";
    }

    //移动邮件
    @PostMapping("/move")
    public String moveEmail(@RequestParam Integer emailId,@RequestParam Integer folderId){
        emailService.moveEmail(emailId,folderId);
        return "移动成功";
    }
    //逻辑删除
    @PostMapping("/logic_delete")
    public String logicDeleteEmail(@RequestParam Integer emailId){
        emailService.moveEmail(emailId,4);
        return "逻辑删除成功";
    }
    //恢复邮件
    @PostMapping("/recover")
    public String recoverEmail(@RequestParam Integer emailId){
        emailService.moveEmail(emailId,3);
        return "恢复成功";
    }
    //彻底删除
    @PostMapping("/delete")
    public String deleteEmail(@RequestParam Integer emailId){
        emailService.deleteEmail(emailId);
        return "彻底删除成功";
    }
    //创建草稿邮件
    @PostMapping("/draft")
    public Integer createDraftEmail(@RequestBody Email email){
        emailService.createDraftEmail(email);
        Integer emailId = email.getEmailId();
        return emailId;
    }
    //更新草稿邮件
    @PostMapping("/update_draft")
    public String updateDraftEmail(@RequestBody Email email){
        emailService.updateDraftEmail(email);
        return "更新成功";
    }
    @PostMapping("/send")
    public String sendEmail(@RequestParam Integer emailId) {
        Email email = emailService.getById(emailId);
        if(email.getIsdraft()==0){
            return "该邮件不是草稿邮件，无法发送";
        }
        emailProducer.sendEmailToQueue(email);
        return "邮件发送请求已提交到队列";
    }
    //点击接收邮件
    @GetMapping("/receive")
    public String receiveEmail(User user){
        User usering = userservice.isExistUser(user);

        if(usering!=null){
            try {
                emailService.receiveEmail(usering);
                return "点击接收";
            }catch (Exception e){
                return "接收失败";
            }

        }else {
            return "用户认证不存在";
        }


    }
    //清空表
    @PostMapping("/clear")
    public String clearEmail(){
        emailService.clearEmail();
        return "清空成功";
    }

    //星标邮件
    @PostMapping("/star")
    public String starEmail(@RequestParam Integer emailId){

        emailService.starEmail(emailId);
        return "星标邮件成功";
    }
    //取消星标
    @PostMapping("/unstar")
    public String unstarEmail(@RequestParam Integer emailId){
        emailService.unStarEmail(emailId);
        return "取消星标成功";
    }
    //标记为已读
    @PostMapping("/read")
    public String readEmail(@RequestParam Integer emailId){
        emailService.readEmail(emailId);
        return "标记为已读成功";
    }
    //标记为未读
    @PostMapping("/unread")
    public String unreadEmail(@RequestParam Integer emailId){
        emailService.unReadEmail(emailId);
        return "标记为未读成功";
    }
    //查询
    @GetMapping("/search")
    public Page<Email> searchEmail(@RequestParam Integer userId,
                                   @RequestParam(required = false) String keyword,
                                   @RequestParam(defaultValue = "1")Integer pageNum,
                                   @RequestParam(defaultValue = "10")Integer pageSize){
        System.out.println("userId:"+userId+"keyword:"+keyword);
        Page<Email> emailPage = emailService.searchEmail(userId,keyword,pageNum,pageSize);

        System.out.println("page记录"+emailPage.getRecords());
        System.out.println("当前页码：" + emailPage.getCurrent());
        System.out.println("每页记录数：" + emailPage.getSize());
        System.out.println("总记录数：" + emailPage.getTotal());
        System.out.println("总页数：" + emailPage.getPages());
        System.out.println("是否有上一页：" + emailPage.hasPrevious());
        System.out.println("是否有下一页：" + emailPage.hasNext());
        return emailPage;
    }

}
